<?php

/**
 * Controller_menu
 */

class Controller_menu extends MyApp {

	public function __construct()
	{
		parent::__construct();

		$this->model->menu = YOU::loadModel('menu');
	}

	/** 首页 **/
	public function actionIndex()
	{

		$this->tpl->display();
	}

	/** 左侧菜单 **/
	public function actionLeft()
	{
		$this->model->menu->arr = $this->model->menu->getFlist();
		$this->model->menu->stt = "";
		$this->model->menu->tree_left(0);
		
		$mu = $this->model->menu->stt;
		
		print_r($mu);
		
		
	}

	/** 菜单列表 **/
	public function actionList()
	{

		$this->model->menu->arr = $this->model->menu->getFlist("all");
		$this->model->menu->stt = "";
		$this->model->menu->tree_list(0);


		$this->tpl->assign("nav_active", "system");
		$this->tpl->assign("menu_active", "menu_list");

		$this->tpl->assign("menu_str", $this->model->menu->stt);
		$this->tpl->display("menu/menu.list.tpl.php");
	}

	/** 菜单添加 **/
	public function actionAdd()
	{
		$menu_fid = (int)$this->get->menu_fid;
		$f_data = $this->db->fetchRow("SELECT * FROM `tbl_menu` WHERE menu_id='{$menu_fid}'");
		if (!empty($f_data)){
			$this->tpl->assign("f_data", $f_data);
		}

		if ($_POST){

			$menu_name  = $this->post->menu_name;
			$menu_fun   = $this->post->menu_fun;
			$menu_url   = $this->post->menu_url;
			$menu_fid   = $this->post->menu_fid;
			$menu_index = $this->post->menu_index;
			$menu_pam   = $this->post->menu_pam;			
			$show_left  = $this->post->show_left;
			$show_index = $this->post->show_index;
			$show_nav   = $this->post->show_nav;

			$info = array(
			"menu_name"   => $menu_name,
			"menu_fun"    => $menu_fun,
			"menu_url"    => $menu_url,
			"menu_fid"    => $menu_fid,
			"menu_index"  => $menu_index,
			"menu_pam"    => $menu_pam,			
			"show_left"   => $show_left,
			"show_index"  => $show_index,
			"show_nav"    => $show_nav,
			"status"      => 1,
			"create_time" => date("Y-m-d H:i:s"),
			"update_time" => "0000-00-00 00:00:00",

			);
			$this->db->do_insert("tbl_menu", $info);

			// 跳转
			Helper_Client::location("添加成功", "index.php?a=menu.list");
		}


		$this->model->menu->arr = $this->model->menu->getFlist();
		$this->model->menu->stt = "";
		$this->model->menu->fid = $menu_fid;
		$this->model->menu->tree_select(0, "");

		$this->tpl->assign("tree_select", $this->model->menu->stt);
		$this->tpl->display("menu/menu.add.tpl.php");
	}


	/** 菜单修改 **/
	public function actionUpdate()
	{
		$menu_id = (int)$this->get->menu_id;
		$menu_data = $this->db->fetchRow("SELECT * FROM `tbl_menu` WHERE menu_id='{$menu_id}' ");
		if (empty($menu_data)){
			Helper_Client::alert("菜单不存在");

		}

		if ($_POST){

			$menu_name  = $this->post->menu_name;
			$menu_fun   = $this->post->menu_fun;
			$menu_url   = $this->post->menu_url;
			$menu_fid   = $this->post->menu_fid;
			$menu_index = $this->post->menu_index;
			$menu_pam   = $this->post->menu_pam;			
			$show_left  = $this->post->show_left;
			$show_index = $this->post->show_index;
			$show_nav   = $this->post->show_nav;

			$info = array(
			"menu_name"   => $menu_name,
			"menu_fun"    => $menu_fun,
			"menu_url"    => $menu_url,
			"menu_fid"    => $menu_fid,
			"menu_index"  => $menu_index,
			"menu_pam"    => $menu_pam,			
			"show_left"   => $show_left,
			"show_index"  => $show_index,
			"show_nav"    => $show_nav,
			"update_time" => date("Y-m-d H:i:s"),

			);
			$this->db->do_update("tbl_menu", $info, "menu_id='{$menu_id}'");

			// 跳转
			Helper_Client::location("修改菜单成功", "index.php?a=menu.list");
		}

		$this->model->menu->arr = $this->model->menu->getFlist();
		$this->model->menu->stt = "";
		$this->model->menu->fid = $menu_data['menu_fid'];
		$this->model->menu->tree_select(0, "");

		$this->tpl->assign("tree_select", $this->model->menu->stt);
		$this->tpl->assign("menu_data", $menu_data);
		$this->tpl->display("menu/menu.update.tpl.php");
	}

	/** 菜单删除 **/
	public function actionDel()
	{
		$menu_id = (int)$this->get->menu_id;

		$sql = "DELETE FROM `tbl_menu` WHERE menu_id='{$menu_id}'";
		$this->db->query($sql);

		// 跳转
		Helper_Client::location("删除成功", "index.php?a=menu.list");

	}

	/** 顺序排序 **/
	public function actionOrder ()
	{
		$menu_lists = $this->db->fetchAll("SELECT menu_id FROM `tbl_menu`");
		foreach ($menu_lists as $key => $value){
			$menu_id = $value['menu_id'];
			if (!empty($_POST["menu_order_{$menu_id}"])){

				$info = array(
				"menu_order" => $_POST["menu_order_{$menu_id}"],
				);

				$this->db->do_update("tbl_menu", $info, "menu_id='{$menu_id}'");

			}

		}


		// 跳转
		Helper_Client::location("排序成功", "index.php?a=menu.list");
	}


}

?>